#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>

static int major = 0;
static int minor = 0;
static int minor_nr = 0;

#define DRAVER_NAME "Simple_cdev"

static struct class *simp_dev_class = NULL;
static struct cdev cdev;
static int simp_open(struct inode *node, struct file *filp)
{
	printk("%s:%d\n", __func__, __LINE__);
	return 0;
}

static int simp_release(struct inode *node, struct file *filp)
{
	printk("%s:%d\n", __func__, __LINE__);
	return 0;
}

static ssize_t simp_read(struct file* filp, char *buf, size_t size, loff_t *f_pos)
{
	return 0;
}

static ssize_t simp_write(struct file* filp, char *buf, size_t size, loff_t *f_pos)
{
	return 0;
}

static struct file_operations simp_ope = {
	.owner = THIS_MODULE,
	.open  = simp_open,
	.read  = simp_read,
	.write = simp_write,
	.release = simp_release
};

int __init simple_cdrv_init(void)
{
	int ret;

	dev_t simp_dev = 0;
	if (alloc_chrdev_region(&simp_dev, minor, minor_nr, DRAVER_NAME) < 0) {
		printk("%s:%d alloc_chardev_region error!\n", __func__, __LINE__);
	}
	major = MAJOR(simp_dev);
	printk("%s:%d Major: %d, Minor: %d\n", __func__, __LINE__, major, minor);

	simp_dev_class = class_create(THIS_MODULE, DRAVER_NAME);

	cdev_init(&cdev, &simp_ope);
	cdev.owner = THIS_MODULE;
	cdev.ops   = &simp_ope;
	ret = cdev_add(&cdev, simp_dev, 1);
	if (ret) {
		printk("%s:%d cdev_add error!\n", __func__, __LINE__);
	}

	device_create(simp_dev_class, NULL, simp_dev, NULL, DRAVER_NAME);

	return 0;
}

static void __exit simple_cdrv_exit(void)
{
}
module_init(simple_cdrv_init);
module_exit(simple_cdrv_exit);
MODULE_LICENSE("GPL");
